import { ref } from 'vue'
export default function () {
  var set_timer
  var time = 0
  let timeText = ref('')
  var pause = ref(true)

  function startTime(from) {
    if (typeof from === 'number') {
      time = from
    }
    clearInterval(set_timer)

    pause.value = false //暂停表示设置为false
    set_timer = setInterval(() => {
      time += 1 //一秒钟加一，单位是秒
      var min = parseInt(time / 60) //把秒转换为分钟，一分钟60秒，取商就是分钟
      var sec = time % 60 //取余就是秒
      timeText.value = (min ? min + '分' : '') + sec + '秒'
    }, 1000)
  }
  const pauseTime = () => {
    pause.value = true
    clearInterval(set_timer)
  }
  const startOrPause = () => {
    if (pause.value) {
      startTime()
    } else {
      pauseTime()
    }
  }

  return {
    timeText,
    startTime,
    pauseTime,
    pause,
    startOrPause
  }
}
